package gov.dhs.cis.soa.readers;

import gov.dhs.cis.soa.SOAData;
import gov.dhs.cis.soa.Utils;
import gov.dhs.cis.soa.data.InfrastructureLS;
import gov.dhs.cis.soa.data.InfrastructurePS;

import java.util.ArrayList;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;


public class InfrastructureLayerReader extends ExcelReader {
	static final Logger loger = Logger.getLogger(InfrastructureLayerReader.class);

	private final String INTEGRATION_LAYER_FILE_NAME = "USCIS Architectural Components - Infrastructure Layer.xls";
	private String LS_SHEET_NAME = "Unique Logical Server Platforms";
	private String PS_SHEET_NAME = "Unique Physical Server Platform";
	
	private String NAME_COL_NUM = "A";
	private String LS_IHOST_COL_NUM = "B";
	private String LS_DESC_COL_NUM = "C";
	private String LS_LS_TYPE_NUM = "D";
	private String LS_HWV_COL_NUM = "E";
	private String LS_HW_COL_NUM = "F";
	private String LS_PROD_COL_NUM = "G";
	private String LS_OSV_COL_NUM = "H";
	private String LS_OS_COL_NUM = "I";
	private String LS_VER_COL_NUM = "J";
	private String LS_REL_COL_NUM = "K";
	private String LS_SUP_SEC_COL_NUM = "L";
	private String LS_INT_SEC_COL_NUM = "M";
	private String LS_DA_COL_NUM = "N";

	private String PS_ST_COL_NUM = "B";
	private String PS_INF_LS_COL_NUM = "C";
	private String PS_DES_COL_NUM = "D";
	private String PS_CHIP_COL_NUM = "E";
	private String PS_ASSOC_COL_NUM = "F";
	private String PS_HWV_COL_NUM = "G";
	private String PS_HW_COL_NUM = "H";
	private String PS_PROD_COL_NUM = "I";
	private String PS_OSV_COL_NUM = "J";
	private String PS_OS_COL_NUM = "K";
	private String PS_VER_COL_NUM = "L";
	private String PS_REL_COL_NUM = "M";
	private String PS_SUP_SEC_COL_NUM = "N";
	private String PS_USED_SEC_COL_NUM = "O";
	private String PS_DA_COL_NUM = "P";

	private int LS_MAX_ROWS = 75;
	private int PS_MAX_ROWS = 115;

	public SOAData extractAll(SOAData soaData) {
		extractLogicalServerComps(soaData);
		extractPhysicalServerComps(soaData);
		return soaData;
	}
	
	private SOAData extractLogicalServerComps(SOAData soaData) {
		loger.debug("Extracting Infrastructure LS layer Sheet...");
		HSSFWorkbook w = openFile(INTEGRATION_LAYER_FILE_NAME);
		HSSFSheet s1 = w.getSheet(LS_SHEET_NAME);
		int rows = s1.getLastRowNum();
		loger.debug("S1 rows =" + rows);
		
		ArrayList<InfrastructureLS> infLSList = new ArrayList<InfrastructureLS>();
		soaData.infLSList = infLSList;
		
		for (int n = 2; n < LS_MAX_ROWS; n++) {
			HSSFRow row = s1.getRow(n);
			if(row == null)
				continue;
			String name = getCellStringValue(w, row, NAME_COL_NUM);
			if(Utils.isEmpty(name)) {
				loger.warn("Infrastructure LS Name is Null/Empty at row : " + n + ", ignoring this row.");
				continue;
			}
			InfrastructureLS iObj = new InfrastructureLS();
			iObj.id = Utils.getNextId();
			iObj.name = name;

			iObj.intHostConfig = getCellStringValue(w, row, LS_IHOST_COL_NUM);
			iObj.descText = getCellStringValue(w, row, LS_DESC_COL_NUM);
			iObj.lsType = getCellStringValue(w, row, LS_LS_TYPE_NUM);
			iObj.hwVendor = getCellStringValue(w, row, LS_HWV_COL_NUM);
			iObj.hw = getCellStringValue(w, row, LS_HW_COL_NUM);
			iObj.product = getCellStringValue(w, row, LS_PROD_COL_NUM);
			iObj.osVendor = getCellStringValue(w, row, LS_OSV_COL_NUM);
			iObj.os = getCellStringValue(w, row, LS_OS_COL_NUM);
			iObj.version = getCellStringValue(w, row, LS_VER_COL_NUM);
			iObj.release = getCellStringValue(w, row, LS_REL_COL_NUM);
			iObj.supSecCap = getCellStringValue(w, row, LS_SUP_SEC_COL_NUM);
			iObj.intSecCap = getCellStringValue(w, row, LS_INT_SEC_COL_NUM);
			iObj.designAspects = getCellStringValue(w, row, LS_DA_COL_NUM);
			
			iObj.createDescription();
			
			infLSList.add(iObj);
		}
		closeAll(w);
		return soaData;
	}
	
	private SOAData extractPhysicalServerComps(SOAData soaData) {
		loger.debug("Extracting Infrastructure PS layer Sheet...");
		HSSFWorkbook w = openFile(INTEGRATION_LAYER_FILE_NAME);
		HSSFSheet s1 = w.getSheet(PS_SHEET_NAME);
		int rows = s1.getLastRowNum();
		loger.debug("S1 rows =" + rows);
		
		ArrayList<InfrastructurePS> infPSList = new ArrayList<InfrastructurePS>();
		soaData.infPSList = infPSList;
		
		for (int n = 2; n < PS_MAX_ROWS; n++) {
			HSSFRow row = s1.getRow(n);
			if(row == null)
				continue;
			String name = getCellStringValue(w, row, NAME_COL_NUM);
			String stereoType = getCellStringValue(w, row, PS_ST_COL_NUM);

			if(Utils.isEmpty(name)) {
				loger.warn("Infrastructure LS Name is Null/Empty at row : " + n + ", ignoring this row.");
				continue;
			}
			if(Utils.isEmpty(stereoType)) {
				loger.warn("Infrastructure LS Stereo Type is Null/Empty at row : " + n + ", ignoring this row.");
				continue;
			}
			InfrastructurePS iObj = new InfrastructurePS();
			iObj.id = Utils.getNextId();
			iObj.name = name;
			iObj.stereoType = stereoType;
			
			iObj.infLS = getCellStringValue(w, row, PS_INF_LS_COL_NUM);
			iObj.descText = getCellStringValue(w, row, PS_DES_COL_NUM);
			iObj.chipset = getCellStringValue(w, row, PS_CHIP_COL_NUM);
			iObj.association = getCellStringValue(w, row, PS_ASSOC_COL_NUM);
			iObj.hwVendor = getCellStringValue(w, row, PS_HWV_COL_NUM);
			iObj.hw = getCellStringValue(w, row, PS_HW_COL_NUM);
			iObj.product = getCellStringValue(w, row, PS_PROD_COL_NUM);
			iObj.osVendor = getCellStringValue(w, row, PS_OSV_COL_NUM);
			iObj.os = getCellStringValue(w, row, PS_OS_COL_NUM);
			iObj.version = getCellStringValue(w, row, PS_VER_COL_NUM);
			iObj.release = getCellStringValue(w, row, PS_REL_COL_NUM);
			iObj.supSecCap = getCellStringValue(w, row, PS_SUP_SEC_COL_NUM);
			iObj.usedSecCap = getCellStringValue(w, row, PS_USED_SEC_COL_NUM);
			iObj.designAspects = getCellStringValue(w, row, PS_DA_COL_NUM);
			
			iObj.createDescription();
			
			infPSList.add(iObj);
		}
		closeAll(w);
		return soaData;
	}
}
